#!/bin/sh

set -e
set -u

echo "Building VirtualBox guest modules"

hw_arch="`dpkg --print-architecture`"
if [ "$hw_arch" != i386 -a "$hw_arch" != amd64 ]; then
   exit 0
fi

. /usr/share/amnesia/build/variables

# the -dkms package must be installed *after* dkms to be properly registered
apt-get install --yes build-essential dkms

# Note: we only build for the 32-bit kernel, since building for 64-bit is too painful
# with multiarch; and anyway, the 64-bit kernel module doesn't play well with
# a 32-bit userspace (https://www.virtualbox.org/ticket/8336), which is why
# we instruct users to set up a 32-bit VM.

# Installing the headers triggers the building of the modules for that kernel
apt-get install --yes \
    "linux-headers-${KERNEL_VERSION}-686" \
    virtualbox-guest-dkms

# clean the build directory
rm -r /var/lib/dkms/virtualbox-guest/

# virtualbox-guest-dkms's postrm script deletes any previously
# built binary module; let's delete it before the package gets purged.
rm /var/lib/dpkg/info/virtualbox-guest-dkms.prerm

# Also copy the udev rules installed by virtualbox-guest-dkms to enable guest
# additions by default.
cp -a /lib/udev/rules.d/60-virtualbox-guest-dkms.rules /etc/udev/rules.d/
